use "/Users/amgbusse/Dropbox/RasterData100x100.dta"

gen Cantoni_refneighbors=., after (Cantoni_refneighbors1621)
replace Cantoni_refneighbors=0 if year<=1517
replace Cantoni_refneighbors=Cantoni_refneighbors1532 if year>=1532 & year<=1539  
replace Cantoni_refneighbors=Cantoni_refneighbors1540 if year>=1540 & year<=1549  
replace Cantoni_refneighbors=Cantoni_refneighbors1548 if year>=1548 & year<=1554
replace Cantoni_refneighbors=Cantoni_refneighbors1555 if year>=1555 & year<=1599  
replace Cantoni_refneighbors=Cantoni_refneighbors1600 if year>=1600 & year<=1619 
replace Cantoni_refneighbors=Cantoni_refneighbors1621 if year>=1621 

gen Rubin_prot=., after (Rubin_prot1600)
replace Rubin_prot=0 if year<=1517
replace Rubin_prot=Rubin_prot1530 if year>=1530 & year<=1559  
replace Rubin_prot=Rubin_prot1560 if year>=1560 & year<=1599  
replace Rubin_prot=Rubin_prot1600 if year>=1600

gen KSP=., after (KS_primogeniture)
replace KSP=0 if KS_primogeniture==0
replace KSP=1 if KS_primogeniture>=.5

gen HRE=.
replace HRE=1 if HRE_Nuessli==1 | HRE_Cantoni ==1


*******TABLE 1 OLS FE
preserve
drop if year<=1450
reghdfe Rubin_prot stateN, a(ID year) vce(cluster ID)
estimates store f1
reghdfe Rubin_prot stateN Cantoni_refneighbors Rubin_press distWITTENBERG UniversitiesAllN, a(ID year) vce(cluster ID)
estimates store f3
reghdfe Rubin_prot stateN UniversitiesAllN MonasteriesN CitiesBosker5kN DO_soilquality, a(ID year) vce(cluster ID)
estimates store f4
reghdfe Rubin_prot stateN BishopricsN VBBCommuneN Cantoni_reichsstadt ParliamentsN KSP HRE, a(ID year) vce(cluster ID)
estimates store f5
reghdfe Rubin_prot stateN Cantoni_refneighbors Rubin_press distWITTENBERG UniversitiesAllN MonasteriesN CitiesBosker5kN DO_soilquality BishopricsN VBBCommuneN Cantoni_reichsstadt ParliamentsN KSP HRE, a(ID year) vce(cluster ID)
estimates store f6
restore

esttab f1 f3 f4 f5 f6 using Table1.rtf, replace se cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) star(* 0.05 ** 0.01 *** 0.001) stats(N, fmt(%9.0fc %9.0fc %9.0fc  %9.0fc)) noomitted nobaselevels label nodepvars collabels(none) legend mtitle("Baseline" "Diffusion" "Resources" "Institutions" "All") title(Fragmentation Facilitates the Protestant Reformation) 

*********Figure 3:
coefplot f6, drop (_cons HRE Cantoni_refneighbors  CitiesBosker5kN KSP) xline (0) order (stateN  UniversitiesAllN MonasteriesN CitiesBosker5kN VBBCommuneN ParliamentsN)  graphregion(fcolor(white)) 
graph save Graph "/Users/amgbusse/Dropbox/Figure2.gph", replace

esttab f1 f3 f4 f5 f6 using Table1.tex, replace se cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) star(* 0.05 ** 0.01 *** 0.001) stats(N, fmt(%9.0fc %9.0fc %9.0fc  %9.0fc)) noomitted nobaselevels label nodepvars collabels(none) legend mtitle("Baseline" "Diffusion" "Resources" "Institutions" "All") title(Fragmentation Faciliates the Protestant Reformation\label{tab1a})


*********TABLE 2 MIXED EFFECTS
preserve
drop if year<=1450
mixed Rubin_prot stateN || ID: 
estimates store f1
mixed Rubin_prot stateN Cantoni_refneighbors Rubin_press distWITTENBERG UniversitiesAllN || ID: 
estimates store f3
mixed Rubin_prot stateN UniversitiesAllN MonasteriesN CitiesBosker5kN DO_soilquality || ID: 
estimates store f4
mixed Rubin_prot stateN BishopricsN VBBCommuneN ParliamentsN KSP HRE || ID: 
estimates store f5
mixed Rubin_prot stateN Cantoni_refneighbors Rubin_press distWITTENBERG UniversitiesAllN MonasteriesN  CitiesBosker5kN DO_soilquality BishopricsN VBBCommuneN Cantoni_reichsstadt ParliamentsN KSP HRE || ID: 
estimates store f6

**********TAble 3 
mixed Rubin_prot Cantoni_reichsstadt ParliamentsN KSP HRE || ID:
estimates store f7
mixed Rubin_prot stateN Cantoni_reichsstadt ParliamentsN KSP HRE || ID: 
estimates store f8
mixed Rubin_prot VBBCommuneN ParliamentsN KSP HRE || ID: 
estimates store f9
mixed Rubin_prot stateN VBBCommuneN ParliamentsN KSP HRE || ID: 
estimates store f10

restore

esttab f1 f3 f4 f5 f6 using Table2.rtf, replace se cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) star(* 0.05 ** 0.01 *** 0.001) stats(N, fmt(%9.0fc %9.0fc %9.0fc  %9.0fc)) noomitted nobaselevels label nodepvars collabels(none) legend mtitle("Baseline" "Diffusion" "Resources" "Institutions" "All") title(Fragmentation Facilitates the Protestant Reformation: ME models) 


esttab f1 f3 f4 f5 f6 using Table2.tex, replace se cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) star(* 0.05 ** 0.01 *** 0.001) stats(N, fmt(%9.0fc %9.0fc %9.0fc  %9.0fc)) noomitted nobaselevels label nodepvars collabels(none) legend mtitle("Baseline" "Diffusion" "Resources" "Institutions" "All") title(Fragmentation Facilitates the Protestant Reformation: ME Models\label{tab1a})

esttab f7 f8 f9 f10 using Table3.rtf, replace se cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) star(* 0.05 ** 0.01 *** 0.001) stats(N, fmt(%9.0fc %9.0fc %9.0fc  %9.0fc)) noomitted nobaselevels label nodepvars collabels(none) legend mtitle("Imperial Cities only" "IC +Fragmentation" "Communes only" "Communes+ Fragmentation" "All") title(Urbanization and the Protestant Reformation: ME models) 

esttab f7 f8 f9 f10 using Table3.tex, replace se cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) star(* 0.05 ** 0.01 *** 0.001) stats(N, fmt(%9.0fc %9.0fc %9.0fc  %9.0fc)) noomitted nobaselevels label nodepvars collabels(none) legend mtitle("Imperial Cities only" "IC +Fragmentation" "Communes only" "Communes+ Fragmentation" "All")title(Urbanization and the Protestant Reformation: ME models) 


***Figure 4 MARGINAL EFECTS OF FRAGMENTATION

preserve
reghdfe Rubin_prot c.stateN##c.Cantoni_refneighbors UniversitiesAllN MonasteriesN CitiesBosker5kN HRE, a(ID year) vce(cluster ID)
estimates store i1
margins, dydx(stateN) at (Cantoni_refneighbors=(0 1))
marginsplot, recast(line) recastci(rarea) ci1opts(fintensity(30)) graphregion(fcolor(white))
graph save Graph "/Users/amgbusse/Dropbox/Marginal neighbors.gph", replace
restore

**Appendix Table A.1: DESCRIPTIVE STATISTICS*************
preserve
estpost summarize Rubin_prot stateN Cantoni_refneighbors Rubin_press distWITTENBERG UniversitiesAllN MonasteriesN CathedralsN BishopricsN CitiesBosker5kN DO_soilquality VBBCommuneN ParliamentsN Cantoni_reichsstadt KSP HRE
esttab using ReformationRasterSummary.rtf, cells("count mean sd min max") label 
esttab using AppTable1.tex, cells("count mean sd min max") label 
restore

***Appendix Table A.2: PLACEBO TESTS***********
**generate placebo: fragmetnation 50 years in future
preserve
drop FstateN
generate FstateN = stateN[_n+10]
drop if year<=1450
reghdfe Rubin_prot FstateN, a(ID year) vce(cluster ID)
estimates store f1
reghdfe Rubin_prot FstateN Cantoni_refneighbors Rubin_press distWITTENBERG UniversitiesAllN, a(ID year) vce(cluster ID)
estimates store f3
reghdfe Rubin_prot FstateN UniversitiesAllN MonasteriesN CitiesBosker5kN DO_soilquality, a(ID year) vce(cluster ID)
estimates store f4
reghdfe Rubin_prot FstateN BishopricsN VBBCommuneN Cantoni_reichsstadt ParliamentsN KSP HRE, a(ID year) vce(cluster ID)
estimates store f5
reghdfe Rubin_prot FstateN Cantoni_refneighbors Rubin_press distWITTENBERG UniversitiesAllN MonasteriesN CitiesBosker5kN DO_soilquality VBBCommuneN Cantoni_reichsstadt ParliamentsN KSP HRE, a(ID year) vce(cluster ID)
estimates store f6
restore
esttab f1 f3 f4 f5 f6 using TablePlacebo.rtf, replace se cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) star(* 0.05 ** 0.01 *** 0.001) stats(N, fmt(%9.0fc %9.0fc %9.0fc  %9.0fc)) noomitted nobaselevels label nodepvars collabels(none) legend mtitle("Baseline" "Diffusion" "Resources" "Institutions" "All") title(Placebo Tests: Future Fragmentation and the Protestant Reformation) 

esttab f1 f3 f4 f5 f6 using AppTable2.tex, replace se cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) star(* 0.05 ** 0.01 *** 0.001) stats(N, fmt(%9.0fc %9.0fc %9.0fc  %9.0fc)) noomitted nobaselevels label nodepvars collabels(none) legend mtitle("Baseline" "Diffusion" "Resources" "Institutions" "All") title(Placebo Tests: Future Fragmentation and the Protestant Reformation) 
restore


**Appendix A.2.a. generate placebo: universities 50 years in future
preserve
drop FUniversity
generate FUniversity = UniversitiesAllN[_n+10]
label var FUniversity "University lead"
drop if year<=1450
reghdfe Rubin_prot stateN Cantoni_refneighbors Rubin_press distWITTENBERG FUniversity, a(ID year) vce(cluster ID)
estimates store f3
reghdfe Rubin_prot stateN FUniversity MonasteriesN CitiesBosker5kN DO_soilquality, a(ID year) vce(cluster ID)
estimates store f4
reghdfe Rubin_prot stateN Cantoni_refneighbors Rubin_press distWITTENBERG UniversitiesAllN MonasteriesN CitiesBosker5kN DO_soilquality FUniversity VBBCommuneN Cantoni_reichsstadt ParliamentsN KSP HRE, a(ID year) vce(cluster ID)
estimates store f6
restore
esttab f3 f4 f6 using TablePlaceboUniversities.rtf, replace se cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) star(* 0.05 ** 0.01 *** 0.001) stats(N, fmt(%9.0fc %9.0fc %9.0fc  %9.0fc)) noomitted nobaselevels label nodepvars collabels(none) legend mtitle("Diffusion" "Resources" "All") title(Placebo Tests: Future Fragmentation and the Protestant Reformation) 

esttab f3 f4 f6 using AppTable2aUniversities.tex, replace se cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) star(* 0.05 ** 0.01 *** 0.001) stats(N, fmt(%9.0fc %9.0fc %9.0fc  %9.0fc)) noomitted nobaselevels label nodepvars collabels(none) legend mtitle("Diffusion" "Resources" "All") title(Placebo Tests: Future Fragmentation and the Protestant Reformation) 

************Appendix TAble A.3: STate level ME**************
use "/Users/amgbusse/Dropbox/StateData.dta"
label var UniversitiesAllN "universities"
label var MonasteriesN "monasteries"
label var CitiesBosker5kN "urbanization"
label var DO_soilquality "ag potential"
label var BishopricsN "bishops"
label var VBB_commune_final_mean "communes"


preserve
drop if year<=1450
mixed Rubin_prot frag100N || year:
estimates store f1
mixed Rubin_prot frag100N Cantoni_refneighbors UniversitiesAllN ||  year:
estimates store f3
mixed Rubin_prot frag100N UniversitiesAllN MonasteriesN CitiesBosker5kN DO_soilquality ||  year:
estimates store f4
mixed Rubin_prot frag100N BishopricsN VBB_commune_final_mean ParliamentsN KSP HRE ||  year:
estimates store f5
mixed Rubin_prot frag100N Cantoni_refneighbors UniversitiesAllN MonasteriesN CitiesBosker5kN DO_soilquality BishopricsN  VBB_commune_final_mean ParliamentsN KSP HRE ||  year:
estimates store f6
restore

esttab f1 f3 f4 f5 f6 using Table3A.rtf, replace se cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) star(* 0.05 ** 0.01 *** 0.001) stats(N, fmt(%9.0fc %9.0fc %9.0fc  %9.0fc)) noomitted nobaselevels label nodepvars collabels(none) legend mtitle("Baseline" "Diffusion" "Resources" "Institutions" "All") title(Fragmentation Faciliates the Protestant Reformation: ME models) 


esttab f1 f3 f4 f5 f6 using Table3A.tex, replace se cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) star(* 0.05 ** 0.01 *** 0.001) stats(N, fmt(%9.0fc %9.0fc %9.0fc  %9.0fc)) noomitted nobaselevels label nodepvars collabels(none) legend mtitle("Baseline" "Diffusion" "Resources" "Institutions" "All") title(Fragmentation Facilitates the Protestant Reformation: ME Models\label{tab1a})

